package com.smartfast4j.manager.modules.file.service.impl;

import com.smartfast4j.common.Constants;
import com.smartfast4j.common.ThreadManager;
import com.smartfast4j.manager.modules.file.service.TestFileManagerService;
import com.smartfast4j.mapper.file.TestFileManagerMapper;
import com.smartfast4j.pojo.file.TestFileManager;
import com.smartfast4j.pojo.file.TestFileManagerExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TestFileManagerServiceImpl implements TestFileManagerService {
    @Autowired
    private TestFileManagerMapper testFileManagerMapper;

    private static final Logger logger = LoggerFactory.getLogger(TestFileManagerServiceImpl.class);

    public int countByExample(TestFileManagerExample example) {
        int count = this.testFileManagerMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public TestFileManager selectByPrimaryKey(Long id) {
        return this.testFileManagerMapper.selectByPrimaryKey(id);
    }

    public List<TestFileManager> selectByExample(TestFileManagerExample example) {
        return this.testFileManagerMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Long id) {
        return this.testFileManagerMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(TestFileManager record) {
        return this.testFileManagerMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(TestFileManager record) {
        return this.testFileManagerMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(TestFileManagerExample example) {
        return this.testFileManagerMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(TestFileManager record, TestFileManagerExample example) {
        return this.testFileManagerMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(TestFileManager record, TestFileManagerExample example) {
        return this.testFileManagerMapper.updateByExample(record, example);
    }

    public int insert(TestFileManager record) {
        return this.testFileManagerMapper.insert(record);
    }

    public int insertSelective(TestFileManager record) {
        return this.testFileManagerMapper.insertSelective(record);
    }

    public int insertBatchSelective(List<TestFileManager> records) {
        return this.testFileManagerMapper.insertBatchSelective(records);
    }

    public int updateBatchByPrimaryKeySelective(List<TestFileManager> records) {
        return this.testFileManagerMapper.updateBatchByPrimaryKeySelective(records);
    }

    @Override
    public void delete(Long loginUserId, List<String> testFileIdList) {
        List<Long> idList = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();
        for(String fileId:testFileIdList){
            String[] tmpArr = fileId.split("#");
            idList.add(Long.parseLong(tmpArr[0]));
            fileNames.add(tmpArr[1]);
        }
        //1.删除资源
        TestFileManager testFileManager = new TestFileManager();
        testFileManager.setUpdateTime(new Date());
        testFileManager.setUpdateUser(loginUserId);
        testFileManager.setDeleteFlag(Constants.DELETE_FLAG.YES);
        TestFileManagerExample example = new TestFileManagerExample();
        example.createCriteria().andIdIn(idList);//要删除的ID
        updateByExampleSelective(testFileManager,example);//逻辑删除
        //异步执行文件删除操作
        ThreadManager.getInstance().createLongPool().execute(() -> {
            //调用文件服务删除物理文件
        });

    }
}